package homework.day02.mapper;

import homework.day02.pojo.User;
import homework.day02.util.IDBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

@Component
public class UserMapper implements IUserMapper {
    @Autowired
    private IDBUtil dbUtil;

    /**
     * 添加用户信息
     *
     * @param user
     * @return
     */
    @Override
    public void insert(User user) {
        System.out.println("开始注册...");
        try (Connection connection = dbUtil.getConnection()) {
            String sql = "INSERT INTO user(username,password,nickname,age) " +
                    "VALUES(?,?,?,?) ";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getNickname());
            ps.setInt(4, user.getAge());
            int num = ps.executeUpdate();
            System.out.println(num > 0 ? "好耶，注册成功咯 O(∩_∩)O" : "糟糕，注册失败啦 ≧ ﹏ ≦");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据用户名查找用户
     *
     * @param userName
     * @return
     */
    @Override
    public User selectByUserName(String userName) {
        try (Connection connection = dbUtil.getConnection()) {
            String sql = "SELECT id,username,password,nickname,age " +
                    "FROM user WHERE username = ? ";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, userName);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                Integer id = rs.getInt("id");
                String username = rs.getString("username");
                String password = rs.getString("password");
                String nickname = rs.getString("nickname");
                Integer age = rs.getInt("age");
                return new User(id, username, password, nickname, age);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据用户名修改用户信息
     */
    @Override
    public void update(String userName,String password,String nickName,Integer age) {
        try (Connection connection = dbUtil.getConnection()) {
            String sql = "UPDATE user " +
                    "SET password = ?,nickname = ?,age = ? " +
                    "WHERE username = ? ";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,password);
            ps.setString(2,nickName);
            ps.setInt(3,age);
            ps.setString(4, userName);
            int num = ps.executeUpdate();
            System.out.println(num > 0 ? "好耶，信息修改成功咯 O(∩_∩)O" : "糟糕，信息修改失败啦 ≧ ﹏ ≦");
        }catch (SQLException e){
            e.printStackTrace();
        }

    }

    @Override
    public List<User> selectAll() {
        List<User> usersList = new ArrayList<>();
        try (Connection connection = dbUtil.getConnection()) {
            String sql = "SELECT id,username,password,nickname,age " +
                    "FROM user ";
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                Integer id = rs.getInt("id");
                String username = rs.getString("username");
                String password = rs.getString("password");
                String nickname = rs.getString("nickname");
                Integer age = rs.getInt("age");
                User user = new User(id, username, password, nickname, age);
                usersList.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return usersList;
    }

    /**
     * 根据用户名删除指定用户的信息
     * @param userName
     */
    @Override
    public void delete(String userName) {
        try (Connection connection = dbUtil.getConnection()) {
            connection.setAutoCommit(false);
            String sql = "DELETE FROM user " +
                    "WHERE username = ? ";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,userName);
            ps.executeUpdate();
            System.out.println("确定要删除该用户吗？（1:确定删除，其它：取消删除）");
            String input = new Scanner(System.in).nextLine();
            if( "1".equals(input)){
                connection.commit();
                System.out.println(userName+"用户删除成功！");
            }else{
                connection.rollback();
                System.out.println("删除功能已取消！");
            }
           connection.setAutoCommit(true);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
